import axios from "axios"
import { setAlert } from "./alert"
import {
  GET_POSTS,
  POST_ERROR,
  UPDATE_LIKES,
  DELETE_POST,
  ADD_POST,
  GET_POST,
  ADD_COMMENT,
  REMOVE_COMMENT
} from "./types"

//get posts
export const getPosts = () => async dispatch => {
  try {
    const res = await axios.get("/api/posts")
    dispatch({
      type: GET_POSTS,
      payload: res.data
    })
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

//add like
export const addLike = post_id => async dispatch => {
  try {
    const res = await axios.put(`/api/posts/like/${post_id}`)
    dispatch({
      type: UPDATE_LIKES,
      payload: { post_id, likes: res.data }
    })
  } catch (err) {

    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

// remove like
export const removeLike = post_id => async dispatch => {
  try {
    const res = await axios.put(`/api/posts/unlike/${post_id}`)
    dispatch({
      type: UPDATE_LIKES,
      payload: { post_id, likes: res.data }
    })
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

// deletePost
export const deletePost = post_id => async dispatch => {
  try {
    await axios.delete(`/api/posts/${post_id}`)
    dispatch({
      type: DELETE_POST,
      payload: post_id
    })
    dispatch(setAlert("Post Removed", "success"))
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

// addPost
export const addPost = formData => async dispatch => {
  try {
    const res = await axios.post(`/api/posts/`, formData)

    dispatch({
      type: ADD_POST,
      payload: res.data
    })
    dispatch(setAlert("Post Created", "success"))
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

// getPost
export const getPost = post_id => async dispatch => {
  try {
    const res = await axios.get(`/api/posts/${post_id}`)
    dispatch({
      type: GET_POST,
      payload: res.data
    })
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}


// add Comment
export const addComment = (post_id,formData) => async dispatch => {
  try {
    const res = await axios.post(`/api/posts/comment/${post_id}`, formData)

    dispatch({
      type: ADD_COMMENT,
      payload: res.data
    })
    dispatch(setAlert("Comment Added", "success"))
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

// delete Comment
export const deleteComment = (post_id,commentId) => async dispatch => {
  try {
   await axios.delete(`/api/posts/comment/${post_id}/${commentId}`)

    dispatch({
      type: REMOVE_COMMENT,
      payload: commentId
    })
    dispatch(setAlert("Comment Removed", "success"))
  } catch (err) {
    dispatch({
      type: POST_ERROR,
      payload: { msg: err.response.statusText, status: err.response.status }
    })
  }
}

